﻿using System.Data;
using Common.ExpressionAnalysis;
using DBA.DataBaseAccess;

namespace BL.ServerBL
{
    partial class ServerRemotingCommon : BL.BRClientRemotingInterface.IBudgetReport
    {
        #region BudgetReport
        public bool BudgetReport(DataTable budgetdata, string user_realname)
        {
            foreach(DataRow dr in budgetdata.Rows)
            {
                string budget_type_name = dr["预算子类别"].ToString().Trim();
                string pay_method_name = dr["开支方式"].ToString().Trim();
                string budget_price = dr["预算总金额"].ToString().Trim();

                if (DBAccess.GetBudgetTypeIdByBudgetTypeName(budget_type_name) == null
                    || DBAccess.GetPayMethodIdByName(pay_method_name) == null
                    || !StringAnalysis.IsNumeric(budget_price)
                    || DBAccess.GetUserIdByRealName(user_realname) == null)
                    return false;
            }

            foreach (DataRow dr in budgetdata.Rows)
            {
                string budget_type_name = dr["预算子类别"].ToString().Trim();
                string pay_method_name = dr["开支方式"].ToString().Trim();

                string budget_type_id = DBAccess.GetBudgetTypeIdByBudgetTypeName(budget_type_name);
                string pay_method_id = DBAccess.GetPayMethodIdByName(pay_method_name);
                string budget_price = dr["预算总金额"].ToString().Trim();
                string budget_name = dr["预算名称"].ToString().Trim();
                string budget_remark = dr["预算备注"].ToString().Trim();
                string user_id = DBAccess.GetUserIdByRealName(user_realname);

                //if (!this.BudgetReport(budget_name, budget_price, pay_method_id,
                //    budget_type_id, budget_remark, user_id, null))
                    return false;
            }

            return true;
        }

        public bool BudgetReport(string budget_range_id, string budget_name, string budget_price,
            string pay_method_id, string purchase_method_id, 
            string budget_type_id, string budget_remark, string user_id, string[,] budget_item_info)
        {
            string budget_approve_needcount = "1";
            if(DBAccess.InsertBudgetData(
                budget_range_id, budget_name, budget_price, budget_price,
                pay_method_id, purchase_method_id, budget_type_id, budget_remark,
                user_id, budget_item_info, budget_approve_needcount))
            {
                string user_name = DBAccess.GetUserNameByUserId(user_id);
                this.RaiseBudgetReported(user_name);
                return true;
            }
            else
                return false;
        }

        private void RaiseBudgetReported(string user_name)
        {
            UserState.RaiseBudgetReported(user_name);
        }

       

        
        #endregion
    }
}
